package com.geeklan.smart.ui.settings;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;

import com.geeklan.base.base.BaseActivity;
import com.geeklan.smart.R;
import com.geeklan.smart.common.Constants;
import com.geeklan.smart.data.model.VersionResponse;
import com.geeklan.smart.data.repository.msg.CommonRepository;
import com.geeklan.smart.databinding.ActivityAboutBinding;
import com.geeklan.smart.ui.main.MainActivity;
import com.geeklan.smart.ui.viewmodel.common.CommonViewModel;
import com.geeklan.smart.ui.viewmodel.common.CommonViewModelFactory;
import com.geeklan.smart.ui.web.WebViewActivity;
import com.geeklan.smart.utils.AppUtils;
import com.geeklan.smart.utils.VersionComparator;

public class AboutActivity extends BaseActivity<ActivityAboutBinding> {
    private CommonViewModel mViewModel;
    private int limit = 20;//每页多少条
    private int page = 1;//页码

    @Override
    protected ActivityAboutBinding initBinding() {
        return ActivityAboutBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        mViewModel = new ViewModelProvider(this, new CommonViewModelFactory(new CommonRepository())).get(CommonViewModel.class);

        mViewModel.getVersionListLiveData().observe(this, response -> {
            dismiss();
            if (response.getData() != null && !response.getData().isEmpty()) {
                VersionResponse versionResponse = response.getData().get(0);
                if(VersionComparator.compareAppVersions(versionResponse.getVersion(),AppUtils.getVersionName(getApplicationContext())) >= 1){
                    mBinding.ivNew.setVisibility(View.VISIBLE);
                    mBinding.tvNewVersion.setText(versionResponse.getVersion());
                    mBinding.llVersion.setOnClickListener(v -> {
                        FunctionActivity.startActivity(this);
                    });
                }
            }
        });

        mViewModel.getVersionErrorLiveData().observe(this, s -> {
            Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
            dismiss();
        });
        mViewModel.getAppVersion(limit,page);
    }



    @Override
    protected void initView() {
        mBinding.tvVersion.setText(AppUtils.getVersionName(getApplicationContext()));
        mBinding.llFunction.setOnClickListener(v -> {
            FunctionActivity.startActivity(this);
        });
        mBinding.ivBack.setOnClickListener(v -> {
            finish();
        });
        mBinding.llVersion.setOnClickListener(v -> {
            loading();
            mViewModel.getAppVersion(limit,page);
        });
        mBinding.llAgreement.setOnClickListener(v -> {
            WebViewActivity.startActivity(this, Constants.USER_URL);
        });
        mBinding.llPrivacyPolicy.setOnClickListener(v -> {
            WebViewActivity.startActivity(this, Constants.PRIVACY_URL);
        });
    }

    @Override
    protected void onApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            getWindow().setStatusBarColor(Color.WHITE);
            return insets;
        });
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }
    public static void startActivity(Context context){
        Intent intent = new Intent(context, AboutActivity.class);
        context.startActivity(intent);
    }
}